unit ufrmSysObjects;

interface

uses
  Classes, Controls, Forms, StdCtrls, cxTL,
  DB, 
  
  
  ufrmChildOne, 
  cxDBTL, Variants, uPubDeclare, 
  cxMaskEdit, cxCheckBox, cxEdit,
  cxDBData, ImgList, cxInplaceContainer, DBClient, Provider, ADODB,
  DosMove, Menus, ExtCtrls, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, cxTLData, cxPC, RzPanel, RzSplit, cxGraphics,
  cxCustomData, cxStyles, cxFilter, cxData, cxDataStorage;

type
  TfrmSysObjects = class(TfrmChildOne)
    lblTitle: TLabel;
    cdblstData: TcxDBTreeList;
    cxDBTreeList1ID: TcxDBTreeListColumn;
    cxDBTreeList1PARENT: TcxDBTreeListColumn;
    cxDBTreeList1PROG_NM: TcxDBTreeListColumn;
    cxDBTreeList1PROG_CLASS: TcxDBTreeListColumn;
    cxDBTreeList1IS_VISIBLE: TcxDBTreeListColumn;
    cxDBTreeList1PARAMS: TcxDBTreeListColumn;
    cxDBTreeList1SORT: TcxDBTreeListColumn;
    ilListItem: TImageList;
    adsDataSO_ID: TLargeintField;
    adsDataPARENT_SO_ID: TLargeintField;
    adsDataPROG_NM: TStringField;
    adsDataPROG_CLASS: TStringField;
    adsDataIS_VISIBLE: TStringField;
    adsDataPARAMS: TStringField;
    adsDataSORT: TBCDField;
    adsDataADD_USER: TLargeintField;
    adsDataADD_TIME: TDateTimeField;
    adsDataEDIT_USER: TLargeintField;
    adsDataEDIT_TIME: TDateTimeField;
    adsDataPICID: TIntegerField;
    cdsDataSO_ID: TLargeintField;
    cdsDataPARENT_SO_ID: TLargeintField;
    cdsDataPROG_NM: TStringField;
    cdsDataPROG_CLASS: TStringField;
    cdsDataIS_VISIBLE: TStringField;
    cdsDataPARAMS: TStringField;
    cdsDataSORT: TBCDField;
    cdsDataADD_USER: TLargeintField;
    cdsDataADD_TIME: TDateTimeField;
    cdsDataEDIT_USER: TLargeintField;
    cdsDataEDIT_TIME: TDateTimeField;
    cdsDataPICID: TIntegerField;
    procedure cdblstDataEditing(Sender: TObject;
      AColumn: TcxTreeListColumn; var Allow: Boolean);
  private
    FLastID: string;
    { Private declarations }
    function GetNewID: string;
    function GetSortID(AParent: string): string;
  protected
    procedure doQuery; override;

    procedure doBeforeOperate(Operate: TOperateState); override;
    procedure doAfterOperate(Operate: TOperateState); override;
  public
    { Public declarations }
  end;

var
  frmSysObjects: TfrmSysObjects;

implementation

uses uPubFuncs;

{$R *.dfm}

{ TfrmSysObjects }

procedure TfrmSysObjects.doQuery;
begin
  inherited;
  if cdsData.Active then cdsData.Refresh
  else cdsData.Open;
end;

function TfrmSysObjects.GetNewID: string;
const
  sSql = 'select max(so_id)+1 from sys_objects';
begin
  Result := VarToStrDef(ExcuteSqlRlt(sSql), '1');
end;

function TfrmSysObjects.GetSortID(AParent: string): string;
const
  sSql = 'select max(sort) + 1 from sys_objects where parent_so_id = %S';
begin
  Result := VarToStrDef(ExcuteSqlRlt(MyFormatStr(sSql, [AParent])), '1');
end;

procedure TfrmSysObjects.doAfterOperate(Operate: TOperateState);
begin
  inherited;
  case Operate of
    mAdd:
      begin
        cdsData.FieldByName('SO_ID').AsString := GetNewID;
        cdsData.FieldByName('PARENT_SO_ID').AsString := FLastID;
        cdsData.FieldByName('SORT').AsString := GetSortID(FLastID);
        cdsData.FieldByName('IS_VISIBLE').AsString := 'Y';
        cdsData.FieldByName('picid').AsInteger := 0;
        cdsData.FieldByName('PROG_NM').FocusControl;
      end;
    mEdit: cdsData.FieldByName('PROG_NM').FocusControl;
  end;
end;

procedure TfrmSysObjects.doBeforeOperate(Operate: TOperateState);
begin
  inherited;
  if Operate = mAdd then FLastID := cdsData.FieldByName('SO_ID').AsString;
end;

procedure TfrmSysObjects.cdblstDataEditing(Sender: TObject;
  AColumn: TcxTreeListColumn; var Allow: Boolean);
begin
  inherited;
  Allow := OperateState in [mAdd, mEdit];
end;

initialization
  RegisterClass(TfrmSysObjects);
finalization
  UnRegisterClass(TfrmSysObjects);

end.

